﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BaiduIntern.Model;
using Microsoft.Phone.Tasks;
using BaiduIntern.Util;

namespace BaiduIntern.View
{
    public partial class DetailView : PhoneApplicationPage
    {
        public DetailView()
        {
            InitializeComponent();

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            IDictionary<string, string> parameters = this.NavigationContext.QueryString;
            string id = parameters["id"];
            var x = (Application.Current as App).Jobs.First(i => i.JobId == id);
            LayoutRoot.DataContext = x;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();

            webBrowserTask.Uri = new Uri("http://tongxue.baidu.com/baidu/" + txtLink.Text, UriKind.Absolute);

            webBrowserTask.Show();
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            WebClient wc = new WebClient();
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
            wc.UploadStringAsync(new Uri("http://tongxue.baidu.com/PersonAction.do?cmd=getJobApplicantStatistics"), "jobIds=" + txtId.Text);
            wc.UploadStringCompleted += (s, ee) =>
            {
                try
                {
                    var result = JsonSerializationHelper.JsonToObject<JsonResult>(ee.Result);
                    if (result.Status == 0 && result.Count != null)
                        this.Dispatcher.BeginInvoke(new Action(() => txtApply.Text = result.Count[0].Count.ToString()));
                    //this.Dispatcher.BeginInvoke(new Action(() => {
                    //    (LayoutRoot.DataContext as InternJob).ApplyNum=result.Count[0].Count;
                    //}));
                    MessageBox.Show("数据已更新!");
                }
                catch
                {

                    MessageBox.Show("连接错误，请检查网络设置!"); ;
                }

            };

        }
    }
}